<?php

class AppController extends Controller {

    var $components = array('Auth', 'Cookie', 'Session');
    var $uses = array('User', 'Group', 'GroupMembership', 'Followship', 'Message');
    var $helpers = array('Time', 'Linker', 'Form', 'Html', 'Session');

    function beforeFilter() {


///////////// auth setting begins   ///////////////////////////////
        $this->Auth->fields = array('username' => 'email', 'password' => 'password');

        $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');

        $this->Auth->loginRedirect = array('controller' => 'users', 'action' => 'home');

        $this->Auth->logoutRedirect = '/';

        $this->Auth->loginError = '用户名或密码错误，请重试。';

        $this->Auth->authError = "对不起，请先登录。";

        $this->Auth->authorize = 'controller';

        $this->Auth->autoRedirect = false;
////////////////////AUTH SETTINGS END///////////////////////////////

///////////////////SMTP MAIL SETTING BEGINS/////////////////////////
        /* SMTP Options */
        /* SMTP Options FOR GOOGLE MAIL
          $this->Email->smtpOptions = array(
          'port'=>'465',
          'timeout'=>'30',
          'host' => 'ssl://smtp.gmail.com',
          'username'=>'stillwyw@gmail.com',
          'password'=>'still5612',
          ); */

        $this->Email->smtpOptions = array(
            'port' => '25',
            'timeout' => '30',
            'host' => 'smtp.163.com',
            'username' => 'stillwyw',
            'password' => 'iknowyou',
        );
        /* Set delivery method */
        $this->Email->delivery = 'smtp';
        $this->Email->from = '<stillwyw@163.com>';

///////////////////SMTP MAIL SETTING END//////////////////
        // current user
        if ($this->Auth->user()) {
            $this->User->recursive = 0;
            $this->current_user = $this->Auth->user();
            $this->current_user = $this->User->findById($this->current_user['User']['id']);
            $this->cuid = $this->current_user['User']['id'];
            $this->cuuid = $this->current_user['User']['uid'];

            $this->set('cuid', $this->cuid);
            $this->set('cuuid', $this->cuuid);
            $this->set('current_user', $this->current_user);
        }
        // user stuffs
        if (isset($this->params['uid'])) {
            $user = $this->User->findById($this->params['uid']);
            if (empty($user)) {
                $user = $this->User->findByUid($this->params['uid']);
            }
            $this->user = $user;
            if ($this->user) {
                $this->uid = $this->user['User']['id'];
                $this->uuid = $this->user['User']['uid'];
                $this->set('user', $this->user);
                $this->set('uuid', $this->uuid);
                $this->set('uid', $this->uid);
                $this->set('is_followed', $this->Followship->find('list', array('conditions' => array(' Followship.user_id' => $this->cuid, "Followship.following_id" => $this->uid))));
            }
        }
        // group stuffs
        if (isset($this->params['gid'])) {
            $this->Group->recursive = 0;
            $group = $this->Group->findByGid($this->params['gid']);
            $this->current_group = $group;
            $this->cgid = $group['Group']['id'];
            $this->cggid = $group['Group']['gid'];
            $this->set('gid', $this->current_group['Group']['id']);
            $this->set('group', $group);
            $this->set('ggid', $this->params['gid']);
        }
        // ownership
        if (isset($this->cuid) && isset($this->uid)) {
            $this->set('own', ($this->cuid == $this->uid));
        }
        
    }

    function isAuthorized() {
        if (!isset ($this->current_user['User'])) {
            return false;
        }
        if ($this->current_user['User']['is_active'] != 1) {
            $this->redirect("/not_activated");
            return false;
        }
		if ($this->current_user['User']['is_blocked']==1) {
			$this->cakeError('user_blocked');
			return false;
		}
        if (isset($this->params['admin'])) {
            if ($this->current_user['User']['is_admin']!=1) {
                $this->cakeError('not_found');
                return false;
            }
        }
        return true;

    }


    function afterFilter() {
        if (isset($this->cuid)) {
            $mail_count = $this->Message->find('count',
                            array('conditions' => array('Message.user_id' => $this->cuid, 'Message.read' => 0)
                    ));
            $this->set('mail_count', $mail_count);
        }
    }

}

?>